//
//  RecordsSearchCell.m
//  TrainTree
//
//  Created by wanghui on 2017/9/20.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "RecordsSearchCell.h"
#import "RecordsCollectionCell.h"
#import "UICollectionViewLeftAlignedLayout.h"
@interface RecordsSearchCell() <UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIView *collectionBgView;




@end
@implementation RecordsSearchCell

- (void)awakeFromNib {
    [super awakeFromNib];

    [self createCollectionView];
    self.array = @[@"运营",@"扫码",@"推广",@"送水",@"营销",@"送红牛",@"物流",@"财务",@"人力",@"送饮料"];
}

- (NSArray *)array
{
    return _array ?: (_array = [NSArray array]);
}
- (void)createCollectionView
{
    UICollectionViewLeftAlignedLayout *flowLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
    
    flowLayout.minimumLineSpacing = 16;
    flowLayout.minimumInteritemSpacing = 8;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 32, 71) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //注册
    [_collectionView registerNib:[UINib nibWithNibName:@"RecordsCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"RecordsCollectionCell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionBgView addSubview:self.collectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = self.array[indexPath.item];
    CGSize size = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12], NSFontAttributeName, nil]];
    return CGSizeMake(size.width + 24, 24);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     RecordsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RecordsCollectionCell" forIndexPath:indexPath];

    cell.recordLabel.text = self.array[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *record = self.array[indexPath.item];
    if (_delegate && [_delegate respondsToSelector:@selector(recordClick:)]) {
        [_delegate recordClick:record];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
